package com.controller;

import com.bean.Enterprise;
import com.bean.Flower;
import com.bean.Page;
import com.service.FlowerService;
import com.service.impl.FlowerServiceImpl;
import com.utils.WebUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

public class EnterFlowerServlet extends BaseServlet {

    private FlowerService flowerService=new FlowerServiceImpl();
    /**
     * 商家查看鲜花
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void queryPageByEnterpriseid(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        int pageNo= WebUtils.parseInt(req.getParameter("pageNo"),1);
        int pageSize=WebUtils.parseInt(req.getParameter("pageSize"), Page.PAGE_SIZE);
        //String enterpriseid=req.getParameter("enterpriseid");
        Enterprise enterprise= (Enterprise) req.getSession().getAttribute("enterprise");
        String enterpriseid=enterprise.getEnterpriseid();
        Page<Flower>page=flowerService.pageByEnterpriseid(pageNo,pageSize,enterpriseid);
        page.setUrl("enterprise/flowerServlet?action=queryPageByEnterpriseid");
        StringBuffer sb=new StringBuffer("enterprise/flowerServlet?action=queryPageByEnterpriseid");
        if(req.getParameter("enterpriseid")!=null){
            sb.append("&enterpriseid=").append(req.getParameter("enterpriseid"));
        }
        page.setUrl(sb.toString());
        req.setAttribute("page",page);
        req.getRequestDispatcher("/page/enterprise/indexFlower.jsp").forward(req,resp);
    }

    /**
     * 修改产品时先获取该产品的鲜花信息
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void getFlower(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取请求参数
        int flowerid=WebUtils.parseInt(req.getParameter("flowerid"),0);
        //调用servkce
        Flower flower= (Flower) flowerService.queryFlowerByFlowerid(flowerid);
        //保存到request
        req.setAttribute("flower",flower);
        //转发
        req.getRequestDispatcher("/page/enterprise/editFlower.jsp").forward(req,resp);
    }

    /**
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void page(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int pageNo=WebUtils.parseInt(req.getParameter("pageNo"),1);
        int pageSize=WebUtils.parseInt(req.getParameter("pageSize"), Page.PAGE_SIZE);
        //调用flowerservice方法
        Page<Flower>page=flowerService.page(pageNo,pageSize);
       // page.setUrl("enterprise/flowerServlet?action=page");
        page.setUrl("enterprise/flowerServlet?action=queryPageByEnterpriseid");
        //
        req.setAttribute("page",page);
        //
        req.getRequestDispatcher("/page/enterprise/indexFlower.jsp").forward(req,resp);
    }

    /**
     * 根据id修改鲜花产品
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       String flower_id=req.getParameter("flowerid");
       Integer flowerid=Integer.parseInt(flower_id);
        String flower_name=req.getParameter("flower_name");
       String p=req.getParameter("price");
       BigDecimal price = new BigDecimal(p);
       String introduce=req.getParameter("introduce");
       String s=req.getParameter("stock");
       Integer stock=Integer.parseInt(s);
       String fid=req.getParameter("fclassid");
       Integer fclassid=Integer.parseInt(fid);
        //Flower flower=WebUtils.copyParamToBean(req.getParameterMap(),new Flower());
        flowerService.updateById(flowerid,flower_name,price,introduce,stock,fclassid);
    //  List<Map<String,Object>>flowers=  flowerService.queryFlowerid(flowerid);
        Flower flower=flowerService.queryFlowerByFlowerid(flowerid);
        System.out.println(flower);
       req.setAttribute("flowers",flower);
        //
        //resp.sendRedirect(req.getContextPath()+"/enterprise/flowerServlet?action=page");
        resp.sendRedirect(req.getContextPath()+"/enterprise/flowerServlet?action=queryPageByEnterpriseid");
    }

    /**
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取请求的参数保存到bean中
        String flower_name=req.getParameter("flower_name");
        String p=req.getParameter("price");
        BigDecimal price = new BigDecimal(p);
        String introduce=req.getParameter("introduce");
        String s=req.getParameter("stock");
        Integer stock=Integer.parseInt(s);
        String fid=req.getParameter("fclassid");
        Integer fclassid=Integer.parseInt(fid);
        Enterprise enterprise= (Enterprise) req.getSession().getAttribute("enterprise");
        String enterpriseid=enterprise.getEnterpriseid();
        String flower_phoro="flower.jpg";
        Integer sales=0;
        flowerService.insertFlower(flower_name,price,introduce,stock,fclassid,enterpriseid,flower_phoro,sales);

       // Flower flower= WebUtils.copyParamToBean(req.getParameterMap(),new Flower());
       // Flower flower=new Flower();
       // WebUtils.copyParamToBean(req.getParameterMap(),flower);
        //调用flowerservice的addFlower()方法保存图书
       // flowerService.addFlower(flower);
        //System.out.println(flower);
        //请求转发
        //req.getRequestDispatcher("/page/flowwer/indexFlower.jsp").forward(req,resp);

       // resp.sendRedirect(req.getContextPath()+"/enterprise/flowerServlet?action=page");
        resp.sendRedirect(req.getContextPath()+"/enterprise/flowerServlet?action=queryPageByEnterpriseid");
    }

    /**
     * 鲜花下架
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取请求参数
      /*  String flowerid=req.getParameter("flowerid");
        int id= 0;
        try {
            id = Integer.getInteger(flowerid);
        } catch (Exception e) {
            e.printStackTrace();
        }*/
        int flowerid=WebUtils.parseInt(req.getParameter("flowerid"),0);

        //调用flowerservice
        flowerService.deleteFlowerByFlowerid(flowerid);
        //
      //  resp.sendRedirect(req.getContextPath()+"/enterprise/flowerServlet?action=page");
        resp.sendRedirect(req.getContextPath()+"/enterprise/flowerServlet?action=queryPageByEnterpriseid");
    }
}
